<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin\Lession;
use App\Admin\Course;
use Input;
class LessionController extends Controller
{
    //列表
    public function index(){
    	//获取数据
    	$data = Lession::select('lession.*','course.course_name') -> leftJoin('course','lession.course_id','=','course.id') -> orderBy('sort','desc') -> get();
    	//显示视图
    	return view('admin.lession.index',['data' => $data]);
    }

	public function add(){
		if(Input::method() == 'POST'){
			//提交处理
			//自动验证
			//接收数据
			$data = Input::all();
			//写入数据表
			$result = Lession::insert([
				'lession_name'    => $data['lession_name'],
				'course_id'       => $data['course_id'],
				'video_time'      => $data['video_time'],
				'HD_video_addr'   => $data['HD_video_addr'],
				'SC_video_addr'   => $data['SC_video_addr'],
				'video_addr'      => $data['video_addr'],
				'sort'            => $data['sort'],
				'description'     => $data['description'],
				'created_at'      => date('Y-m-d H:i:s'),
				'status'          => 2,
				'cover_img'       => $data['cover_img'],
				'banner_img'      => $data['banner_img'],
				'carousel_img'    => $data['carousel_img'],
				'is_banner_top'   => $data['is_banner_top'],
				'price'           => $data['price'],
				'free'            => $data['free'],
				'courseBelong'    => $data['courseBelong'],
				'click_num'       => $data['click_num'],
				'is_independent_broadcast' => $data['is_independent_broadcast'],
				'is_trailer'      => $data['is_trailer'],
			]);
			session(['cover_img_url' => null]);
			session(['banner_img_url' => null]);
			session(['carousel_img_url' => null]);
			session(['video_url' => null]);
			session(['HD_url' => null]);
			session(['SC_url' => null]);
			//返回
			return $result ? '1' : '0';
		}else{
			$course_id = Input::get('course_id');
			empty($course_id) == true ? $course_id  = 0 : true;
			$courseList = Course::select('id','course_name') -> get();
			return view('admin.lession.add',['courseList' => $courseList ,'course_id' => $course_id]);
		}
	}

	public function edit(){
		if(Input::method() == 'POST'){
			$data   = Input::all();
			$result = Lession::where('id',$data['id']) -> update([
					'lession_name'    => $data['lession_name'],
					'course_id'       => $data['course_id'],
					'video_time'      => $data['video_time'],
					'HD_video_addr'   => $data['HD_video_addr'],
					'SC_video_addr'   => $data['SC_video_addr'],
					'video_addr'      => $data['video_addr'],
					'sort'            => $data['sort'],
					'description'     => $data['description'],
					'created_at'      => date('Y-m-d H:i:s'),
					'status'          => $data['status'],
					'cover_img'       => $data['cover_img'],
					'banner_img'      => $data['banner_img'],
					'carousel_img'    => $data['carousel_img'],
					'is_banner_top'   => $data['is_banner_top'],
					'price'           => $data['price'],
					'free'            => $data['free'],
					'courseBelong'    => $data['courseBelong'],
					'click_num'       => $data['click_num'],
					'is_independent_broadcast' => $data['is_independent_broadcast'],
					'is_trailer'      => $data['is_trailer'],
			]);
			session(['cover_img_url' => null]);
			session(['banner_img_url' => null]);
			session(['carousel_img_url' => null]);
			session(['video_url' => null]);
			session(['HD_url' => null]);
			session(['SC_url' => null]);
			return $result ? '1' : '0';
		}else{
			$id    = Input::get('id');
			$data  = Lession::where("id",$id) -> first();
			$courseList = Course::select('id','course_name') -> get();
			return view('admin.lession.edit',['courseList' => $courseList ,'data' => $data]);
		}
	}

	public function del(){
		$id = Input::get('id');
		$result = Lession::where('id',$id) -> delete();
		return $result ? '1' : '0';
	}

	public function searchcname(){
		$course_name = Input::get('course_name');
		$courseList = Course::select('id','course_name') -> where('course_name','LIKE',"%$course_name%") -> get() -> toArray();
		if(empty($courseList) == true){
			$data = [
				'status' => 0,
				'msg'    => '无数据'
			];

		}else{
			$data = [
					'status' => 1,
					'msg'    => 'success',
					'info'   => $courseList
			];
		}
		return response() -> json($data);
	}

    //播放视频
    public function play(){
    	//获取点播的id
    	$id = Input::get('id');
    	//获取视频地址
    	$v = Lession::where('id',$id) -> first();
    	//地址
    	$addr = $v -> video_addr;
    	//播放视频
    	echo "<video src='$addr' autoplay='autoplay' width='100%'></video>";
    }
}
